package cn.edu.ujn.multiple.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.multiple.model.Product;
import cn.edu.ujn.multiple.service.IProductService;
@Controller
public class ProductController {
	@Autowired
	private IProductService productService;
	
	@GetMapping("/products")
	public String toOrder() {
		return "products";
	}
	
	@RequestMapping("/findAllProducts")
	public String findAll(Model m) {
		List<Product> list = productService.findAll();
		m.addAttribute("productList", list);
		return "products";
	}
	
	@GetMapping("/addProduct")
	public String addProduct() {
		return "addProduct";
	}
	
	@PostMapping("/addProduct")
	public String addProduct(Product p) {
		productService.insert(p);
		System.out.println(p);
		return "products";
	}
	
	@GetMapping("/deleteProduct")
	public String deleteProduct(@RequestParam("id") int id) {
		System.out.println("id为"+id);
		productService.delete(id);
		return "products";
	}
	
	@GetMapping("/updateProduct")
	public String updateProduct(@RequestParam("id") int id, Model m) {
		Product p = productService.selectByPrimaryKey(id);
		m.addAttribute("product", p);
		return "updateProduct";
	}
	
	@PostMapping("/updateProduct")
	public String updateProduct(Product p) {
		System.out.println(p);
		productService.update(p);
		return "products";
	}
	
}
